package com.beiding.utils;

public class MathUtils {


    private static final int EARTH_RADIUS = 6371004;

    //弧度转角度
    private static final double RADIAN_TO_ANGLE;

    //角度转弧度
    private static final double ANGLE_TO_RADIAN;

    static {
        RADIAN_TO_ANGLE=180/Math.PI;
        ANGLE_TO_RADIAN=Math.PI/180;
    }

    public static Border toBorder(double longitude, double latitude, double halfLength){

        //计算跨度
            //计算
        //经度弧
        double lon = longitude * ANGLE_TO_RADIAN;

        //纬度弧
        double lat = latitude * ANGLE_TO_RADIAN;

        //纬度跨度
        double latS = halfLength / EARTH_RADIUS * RADIAN_TO_ANGLE;

        //经度跨度
        double lonS = latS / Math.cos(lat);

        return new Border(longitude,latitude,longitude-lonS,longitude+lonS,latitude-latS,latitude+latS);
    }

    public static class Border
    {

        //经线左边界
        private double minLongitude;

        //经线右边界
        private double maxLongitude;

        //纬线下边界
        private double minLatitude;

        //纬线上边界
        private double maxLatitude;

        //中心
        private double midLongitude;

        private double midLatitude;

        private Border(double midLongitude, double midLatitude, double minLongitude, double maxLongitude, double minLatitude, double maxLatitude) {

            this.minLongitude = minLongitude >= -180 ? minLongitude : minLongitude + 360;
            this.maxLongitude = maxLongitude < 180 ? maxLongitude : maxLongitude - 360;
            this.minLatitude = minLatitude;
            this.maxLatitude = maxLatitude;
            this.midLatitude = midLatitude;
            this.midLongitude = midLongitude;
        }

        public double getMinLongitude() {
            return minLongitude;
        }

        public double getMaxLongitude() {
            return maxLongitude;
        }

        public double getMinLatitude() {
            return minLatitude;
        }

        public double getMaxLatitude() {
            return maxLatitude;
        }

        public double getMidLongitude() {
            return midLongitude;
        }

        public double getMidLatitude() {
            return midLatitude;
        }

        @Override
        public String toString() {
            return "{" +
                    "左边界:" +minLongitude+
                    ",右边界:" +maxLongitude+
                    ",上边界:" +minLatitude+
                    ",下边界:" +maxLatitude+
                    "}";
        }
    }



}
